package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.SeeAll;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.SeeAllQueryObject;
import cn.wolfcode.trip.base.query.StrategyListObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.domain.SeeAll;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ISeeAllService;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.*;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.plugin2.os.windows.SECURITY_ATTRIBUTES;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

   @Autowired
   private IStrategyService strategyService;
   @Autowired
   private IStrategyCatalogService strategyCatalogService;
   @Autowired
   private IStrategyDetailService strategyDetailService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyReplyService strategyReplyService;
    @Autowired
    private ISeeAllService seeAllService;
    /*
     * 获取
     * 资源:/strategies
     * 动作:GET
     * 参数:state=2&pageSize=0
     * */
    @GetMapping
    public Object getCommends(StrategyListObject qo){
        return strategyService.query(qo);
    }


    /*
    * 根据攻略的id查询大攻略
    * 资源:/strategies/{id}
    * 动作:get
    * 参数:
    * */
    @GetMapping("/{id}")
    public Object getStrateById(@PathVariable Long id){
        return strategyService.get(id);
    }

    @PostMapping("/{strategyId}/comment")
    public Object saveComment(String[] labels,String[] images,String star,String content,@PathVariable Long strategyId,Long userId){
        JSONResult jsonResult = new JSONResult();
        StrategyComment strategyComment = new StrategyComment();
        strategyComment.setContent(content);
        strategyComment.setStar(star);
        Strategy strategy = new Strategy();
        strategy.setId(strategyId);
        User user = new User();
        user.setId(userId);
        strategyComment.setStrategy(strategy);
        strategyComment.setUser(user);
        try{
            strategyCommentService.save(strategyComment,labels,images);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //获取所有点评
    @GetMapping("/{strategyId}/comment")
    public Object getComments(StrategyCommentQueryObject qo){
        qo.setPageSize(0);
        PageInfo pageInfo = strategyCommentService.selectForList(qo);
        return pageInfo;
    }

    //获取单个点评具体信息
    @GetMapping("/comment/{id}")
    public Object getCommentMessage(@PathVariable Long id){
        return strategyCommentService.getCommentMessage(id);
    }

    /*
     * 根据某个大攻略下的攻略分类
     * 资源:/strategies/{id}/catalogs
     * 动作:get
     * 参数:
     * */
    @GetMapping("/{strategyId}/catalogs")
    public Object getCatalogsByStrateById(@PathVariable Long strategyId){
        return strategyCatalogService.selectByStrategyId(strategyId);
    }

    /*
     * 根据某个攻略下的文章内容
     * 资源:/strategies/details/{id}
     * 动作:get
     * 参数:
     * */
    @GetMapping("/details/{id}")
    public Object getDetailsById(@PathVariable Long id){
        return strategyDetailService.get(id);
    }

    //查询一个攻略中所有用户点击的标签
    @GetMapping("/{strategyId}/comment/labels")
    public Object getLabels(@PathVariable Long strategyId){
        LabelQueryObject qo = new LabelQueryObject();
        qo.setStrategyId(strategyId);
        qo.setPageSize(10);
        return strategyCommentService.getLabelsByStrategyId(qo);
    }

    //查询一条点评中所有的评论(测试用)
    @GetMapping("/{commentId}/replay")
    public Object getRelayContent(@PathVariable Long commentId){
        return strategyReplyService.listCommentById(commentId);
    }

    //查询一条点评中所有的评论,并且回显在页面
    @GetMapping("/{commentId}/commentreplay")
    public Object getCommentreplay(StrategyReplyQueryObject qo){
        qo.setPageSize(0);
        PageInfo pageInfo = strategyReplyService.selectForList(qo);
        return pageInfo;
    }

    //添加一条评中评
    @PostMapping("/replaycomment/{commentId}")
    public Object saveReplayComment(String content,Long userId,@PathVariable Long commentId){
        JSONResult jsonResult = new JSONResult();
        StrategyReply strategyReply = new StrategyReply();
        StrategyComment strategyComment = new StrategyComment();
        strategyComment.setId(commentId);
        strategyReply.setComment(strategyComment);
        User user = new User();
        user.setId(userId);
        strategyReply.setUser(user);
        strategyReply.setContent(content);
        try{
            strategyReplyService.saveReplay(strategyReply);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    //点评头条显示
    @GetMapping("/headlines")
    public Object getCommentHeadlines(StrategyCommentQueryObject qo){
        qo.setPageSize(0);
        PageInfo pageInfo = strategyCommentService.selectForHeadlines(qo);
        return pageInfo;
    }

    //查询评中评的总条数
    @GetMapping("/{id}/countId")
    public Object getCountId(@PathVariable Long id){
        return strategyReplyService.selectCountById(id);
    }

    /*
     * 根据某个大攻略下的游记
     * 资源:/strategies/{strategyId}/catalogs
     * 动作:get
     * 参数:
     * */
    @GetMapping("/{strategyId}/travels")
    public Object getTravelsByStrateById(SeeAllQueryObject qo){
        PageInfo<SeeAll> query = seeAllService.query(qo);
        return query;
    }

    /*
    * 根据攻略的id修改状态collect为收藏
    * 资源:/strategies/{id}
    * 动作:PUT
    * 参数:collect=1/0
    * */
    @PutMapping("/{id}")
    public Object updateCollectById(@PathVariable Long id, Integer collect, Long userId){
        strategyService.updateCollectById(id, collect, userId);
        return new JSONResult();
    }

}
